﻿using ClinicSystem.configure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ClinicSystem.FormPage
{
	public partial class Login : Form
	{
		public Login()
		{
			InitializeComponent();
			IniAutoComplte();




			pictureBox_DisplayPaw.MouseEnter += (s, e) =>
			{
				pictureBox_DisplayPaw.BackColor = Color.PowderBlue;
			};
			pictureBox_DisplayPaw.MouseLeave += (s, e) =>
			{
				pictureBox_DisplayPaw.BackColor = Color.White;
			};
		}

		private void IniAutoComplte()
		{
			List<string> state = ReadState();
			//string[] s = new string[state.Count];
			foreach (string users in state)
			{
                comboBox_User.Items.Add(users);
            }
			
		}



		string username;
		string password;

		private void button_Login_Click(object sender, EventArgs e)
		{

			username = comboBox_User.Text.Trim();
			password = textBox_Password.Text.Trim();
			userLog(username, password);
			
			
		}
		private void userLog(string username,string password)
		{
			List<string> result = Load_OutFile.CmdQuery("SELECT password FROM doctor WHERE account='" + username + "'");
			if (result.Count > 0 && result[0] == password)
			{
				label_ErrInfo.Text = "登陆成功";
			}
			else if (result.Count > 0 && result[0] != password)

			{
				label_ErrInfo.Text = "密码错误";
				return;
			}
			else if (result.Count < 1)
			{
				label_ErrInfo.Text = "未查询到此用户";
			}
			else if (string.IsNullOrEmpty(password))
			{
				label_ErrInfo.Text = "请输入密码";
				return;
			}
			GlobalVar.currentUser = textBox_peoplename.Text;
			DialogResult = DialogResult.OK;
		}

		private void button_Register_Click(object sender, EventArgs e)
		{
			username = comboBox_User.Text.Trim();
			password = textBox_Password.Text.Trim();
			string peoplename=textBox_peoplename.Text.Trim();
			if (!Regex.IsMatch(username, @"^1[3-9]\d{9}$"))
			{
				label_ErrInfo.Text = "请使用手机号注册";
				return;
				
			}
			
			RegisterNewUser(username, password,peoplename);
			//label_ErrInfo.Text = "注册成功";
		}

		bool i = false;
		private void pictureBox_DisplayPaw_Click(object sender, EventArgs e)
		{

			if (i == false)
			{
				pictureBox_DisplayPaw.Image = Properties.Resources.eysclose;
				textBox_Password.PasswordChar = '\0';
				i = true;
			}
			else
			{
				pictureBox_DisplayPaw.Image = Properties.Resources.eysopen;
				textBox_Password.PasswordChar = '*';
				i = false;
			}

		}


		private List<string> ReadState()
		{
			List<string> name = Load_OutFile.CmdQuery("SELECT name FROM doctor WHERE state='1'");
			List<string> account= Load_OutFile.CmdQuery("SELECT account FROM doctor WHERE state='1'");
			//List<string> result = new List<string>();
			//for(int i=0;i< name.Count;i++)
			//{
			//	result.Add(name[i]+" " + account[i]);
			//}
			return account;
		}

		private void RegisterNewUser(string username, string password,string peoplename)
		{
			List<string>doctorlist= Load_OutFile.CmdQuery("SELECT name FROM doctor WHERE name='"+username+"'");
			if(doctorlist.Count>0)
			{
				MessageBox.Show("用户名已存在");
			}
			List<string> colname = new List<string> { "account","password","name" };
			List<string>value=new List<string> { "'"+username+"'","'"+password+"'","'"+peoplename+"'" };
			Load_OutFile.InsertData("doctor", colname, value);
			label_ErrInfo.Text = "注册成功！";
		}

        private void comboBox_User_TextChanged(object sender, EventArgs e)
        {
            username = comboBox_User.Text;
            List<string> displayname = new List<string>();

            if (Regex.IsMatch(username, @"^1[3-9]\d{9}$"))
            {
                displayname = Load_OutFile.CmdQuery("SELECT name FROM doctor WHERE account='" + username + "'");
                textBox_peoplename.Text = displayname[0];
            }
        }

		private void textBox_Password_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				username = comboBox_User.Text.Trim();
				password = textBox_Password.Text.Trim();
				userLog(username, password);
			}
		}
	}
}
